export default class IFileClient {
	_genTokenUrl = 'genFileConfig'
	_fileConfig = null

	constructor(configData) {
		this._fileConfig = this.genFileConfig(configData)
		console.log('[Debug][File][Init]File config: ', this._fileConfig)
	}

	getConfig() {
		return this._fileConfig
	}
	// 文件列表，格式 name1: url1|name2: url2
	// 返回
	// isFullUrl === true  --> [{name: fileName, url: fileFullUrl}]
	// isFullUrl === false  --> [{name: fileName, url: fileUrl}]
	getFileList(fileStr, isFullUrl) {
		if (!fileStr || fileStr === '') {
			return null
		}

		if (this._fileConfig === null) {
			console.log('[Error][File][getFileList]no file config info.')
			return null
		}

		if (!this._fileConfig.setting.imgUrl || this._fileConfig.setting.imgUrl === null) {
			console.log('[Error][File][getFileList] no deleteUrl.')
			return null
		}

		const fileList = []
		let fileListTmp = fileStr.split(';')

		for (let i = 0; i < fileListTmp.length; i++) {
			let item = fileListTmp[i].split(':=')
			let name = ''
			let url = ''
			if (item.length === 1) {
				name = this.getFileID(item[0])
				url = item[0]
			} else if (item.length >= 2) {
				name = item[0]
				url = item[1]
			} else {
				console.error('[Error][File][getFileList]file item invalid: ', fileListTmp[i])
				continue
			}
			if (isFullUrl) {
				fileList.push({ name: name, url: this.getFileFullUrl(url) })
			} else {
				fileList.push({ name: name, url: url })
			}
		}

		return fileList
	}

	// 接口函数
	getFileID(fileUrl) {
		console.error('[Error][File][getFileID]: ', fileUrl)
		return null
	}
	getFileFullUrl(fileID) {
		console.error('[Error][File][getFileFullUrl]: ', fileID)
		return null
	}
	async uploadContent(fileName, content) {
		console.error('[Error][File][uploadContent]: ', fileName, content)
		return null
	}
	async deleteFile(url) {
		console.error('[Error][File][deleteFile]: ', url)
		return false
	}
	async checkToken() {
		return true
	}

	// 内部函数
	genFileConfig(fileConfigData) {
		if (!fileConfigData) {
			return null
		}

		const fileConfig = { ftpType: fileConfigData.FtpType, postData: {} }
		fileConfig.setting = {
			uploadUrl: fileConfigData.UploadUrl,
			imgUrl: fileConfigData.ImagUrl,
			deleteUrl: fileConfigData.DeleteUrl,
			Region: fileConfigData.Region,
			Bucket: fileConfigData.Bucket
		}
		return fileConfig
	}
}

// export default IFileClient
